package com.unohacha.qqj.fzdr.personal.view.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.bean.WuliuCompany;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.util.ToastUtils;
import com.unohacha.qqj.fzdr.view.permissions.RxPermissions;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class KuaidiCompanyActivity extends BaseActivity implements OnRefreshListener {

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    @BindView(R.id.title_ll)
    LinearLayout titleLl;
    @BindView(R.id.common_cancle)
    ImageView commonCancle;
    @BindView(R.id.edt_seach)
    EditText edtSeach;
    @BindView(R.id.common_right_search)
    TextView commonRightSearch;
    @BindView(R.id.search_ll)
    LinearLayout searchLl;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private List<WuliuCompany> list;
    private MyAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_kuaidi_company;
    }

    @Override
    protected void initView() {
        commonTitle.setText("快递公司");
        list = new ArrayList<>();
        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new MyAdapter(R.layout.item_kuaidi_company, list);
        recyclerview.setAdapter(adapter);
        getMessage("");
    }

    private void getMessage(String info) {
        RetrofitManager.getInstance()
                .WuliuCompany(info)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Action1<HttpResult<List<WuliuCompany>>>() {
                    @Override
                    public void call(HttpResult<List<WuliuCompany>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            list.addAll(listHttpResult.getData());
                            adapter.notifyDataSetChanged();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        list.clear();
        getMessage("");
    }

    private class MyAdapter extends BaseQuickAdapter<WuliuCompany, BaseViewHolder> {
        public MyAdapter(int layoutResId, @Nullable List<WuliuCompany> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, WuliuCompany item) {
            Glide.with(mContext)
                    .load(item.getLogo())
                    .override(70, 45)
                    .into((ImageView) helper.getView(R.id.image));
            helper.setText(R.id.tv_wuliu_name, item.getName());
            helper.setText(R.id.tv_people_name, "总部电话：" + item.getMobile());
            final String phone = item.getMobile();
            helper.getView(R.id.image_phone).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RxPermissions rxPermissions = new RxPermissions((Activity) mContext);
                    rxPermissions.request(Manifest.permission.CALL_PHONE).subscribe(new Observer<Boolean>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(Boolean aBoolean) {
                            if (aBoolean) {
                                if (null != phone) {
                                    Intent intent = new Intent(Intent.ACTION_CALL);
                                    Uri data = Uri.parse("tel:" + phone);
                                    intent.setData(data);
                                    if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                                        // TODO: Consider calling
                                        //    ActivityCompat#requestPermissions
                                        // here to request the missing permissions, and then overriding
//                                           public void onRequestPermissionsResult(int requestCode, String[] permissions,
//                                                                                  int[] grantResults)
                                        // to handle the case where the user grants the permission. See the documentation
                                        // for ActivityCompat#requestPermissions for more details.
                                        return;
                                    }
                                    startActivity(intent);
                                }
                            } else {
                                ToastUtils.showT("照相机权限未获得");
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
                }
            });
        }
    }

    @OnClick({R.id.common_back, R.id.common_right_image, R.id.common_cancle, R.id.common_right_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                finish();
                break;
            case R.id.common_right_image://搜索图片
                searchLl.setVisibility(View.VISIBLE);
                edtSeach.setText("");
                titleLl.setVisibility(View.GONE);
                break;
            case R.id.common_cancle://取消
                searchLl.setVisibility(View.GONE);
                titleLl.setVisibility(View.VISIBLE);
                edtSeach.setText("");
                break;
            case R.id.common_right_search://搜索按钮
                if (!TextUtils.isEmpty(edtSeach.getText().toString().trim())) {
//                    getAppMaterialData(edtSeach.getText().toString().trim());
                    getMessage(edtSeach.getText().toString().trim());
                } else {
                    MyToast("请输入搜索内容!");
                    return;
                }
                break;
        }
    }
}
